﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class LocationInventoryDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static LocationInventoryDO GetModelByPK(Session sess, int iIntLocInv)
        {
            DataHelper helper = null;
            DataTable dtModel = null;

            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, SelectSequel + " WHERE INTERNAL_LOCATION_INV = @INTERNAL_LOCATION_INV", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_LOCATION_INV", iIntLocInv)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_LOCATION_INV也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public void Insert(Session sess, LocationInventoryDO locInvDO)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "INV_IInventory";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(locInvDO);
                locInvDO.INTERNAL_LOCATION_INV = helper.Insert(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public void Delete(Session sess, LocationInventoryDO locInvDO)
        {
            DataHelper helper = null;
            string stSQL = "INV_DInventory";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(locInvDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess, LocationInventoryDO locInvDO)
        {
            DataHelper helper = null;
            string stSQL = "INV_UInventory";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(locInvDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象LOCATIONINVENTORY的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        [Obsolete]
        public static List<LocationInventoryDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, SelectSequel);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性LOCATIONINVENTORY对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        public static List<LocationInventoryDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
            }
        }
  
        /// <summary>
        /// 从DataView装载持久性LOCATIONINVENTORY对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<LocationInventoryDO> LoadListFromDataView(DataView dv)
        {
            List<LocationInventoryDO> list = new List<LocationInventoryDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static LocationInventoryDO GetModel(DataRow row)
        {
            LocationInventoryDO locInvDO = new LocationInventoryDO();
            if (!row["INTERNAL_LOCATION_INV"].Equals(DBNull.Value)) locInvDO.INTERNAL_LOCATION_INV = Convert.ToInt32(row["INTERNAL_LOCATION_INV"]);
            if (!row["ITEM"].Equals(DBNull.Value)) locInvDO.ITEM = Convert.ToString(row["ITEM"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) locInvDO.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["PERMANENT"].Equals(DBNull.Value)) locInvDO.PERMANENT = Convert.ToString(row["PERMANENT"]);
            if (!row["ATTRIBUTE_NUM"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE_NUM = Convert.ToString(row["ATTRIBUTE_NUM"]);
            if (!row["ON_HAND_QTY"].Equals(DBNull.Value)) locInvDO.ON_HAND_QTY = Convert.ToInt32(row["ON_HAND_QTY"]);
            if (!row["IN_TRANSIT_QTY"].Equals(DBNull.Value)) locInvDO.IN_TRANSIT_QTY = Convert.ToInt32(row["IN_TRANSIT_QTY"]);
            if (!row["ALLOCATED_QTY"].Equals(DBNull.Value)) locInvDO.ALLOCATED_QTY = Convert.ToInt32(row["ALLOCATED_QTY"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) locInvDO.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["INVENTORY_STS"].Equals(DBNull.Value)) locInvDO.INVENTORY_STS = Convert.ToString(row["INVENTORY_STS"]);
            if (!row["AGING_DATE"].Equals(DBNull.Value)) locInvDO.AGING_DATE = Convert.ToDateTime(row["AGING_DATE"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) locInvDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) locInvDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) locInvDO.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["LOCATION"].Equals(DBNull.Value)) locInvDO.LOCATION = Convert.ToString(row["LOCATION"]);
            if (!row["ITEM_DESC"].Equals(DBNull.Value)) locInvDO.ITEM_DESC = Convert.ToString(row["ITEM_DESC"]);
            if (!row["LPN"].Equals(DBNull.Value)) locInvDO.LPN = Convert.ToString(row["LPN"]);
            if (!row["PARENT_LPN"].Equals(DBNull.Value)) locInvDO.PARENT_LPN = Convert.ToString(row["PARENT_LPN"]);
            if (!row["ATTRIBUTE1"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE1 = Convert.ToString(row["ATTRIBUTE1"]);
            if (!row["ATTRIBUTE2"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE2 = Convert.ToString(row["ATTRIBUTE2"]);
            if (!row["ATTRIBUTE3"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE3 = Convert.ToString(row["ATTRIBUTE3"]);
            if (!row["ATTRIBUTE4"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE4 = Convert.ToString(row["ATTRIBUTE4"]);
            if (!row["ATTRIBUTE5"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE5 = Convert.ToString(row["ATTRIBUTE5"]);
            if (!row["ATTRIBUTE6"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE6 = Convert.ToString(row["ATTRIBUTE6"]);
            if (!row["ATTRIBUTE7"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE7 = Convert.ToString(row["ATTRIBUTE7"]);
            if (!row["ATTRIBUTE8"].Equals(DBNull.Value)) locInvDO.ATTRIBUTE8 = Convert.ToString(row["ATTRIBUTE8"]);

            return locInvDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [LOCATION_INVENTORY] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(LocationInventoryDO locInvDO)
        {
            SqlParameter[] paras = new SqlParameter[54];
            paras[0] = new SqlParameter("@INTERNAL_LOCATION_INV", locInvDO.INTERNAL_LOCATION_INV);
            paras[1] = new SqlParameter("@ITEM", locInvDO.ITEM == String.Empty ? DBNull.Value : (object)locInvDO.ITEM);
            paras[2] = new SqlParameter("@COMPANY", locInvDO.COMPANY == String.Empty ? DBNull.Value : (object)locInvDO.COMPANY);
            paras[3] = new SqlParameter("@PERMANENT", locInvDO.PERMANENT == String.Empty ? DBNull.Value : (object)locInvDO.PERMANENT);
            paras[4] = new SqlParameter("@ATTRIBUTE_NUM", locInvDO.ATTRIBUTE_NUM == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE_NUM);
            paras[5] = new SqlParameter("@ON_HAND_QTY", locInvDO.ON_HAND_QTY);
            paras[6] = new SqlParameter("@IN_TRANSIT_QTY", locInvDO.IN_TRANSIT_QTY);
            paras[7] = new SqlParameter("@ALLOCATED_QTY", locInvDO.ALLOCATED_QTY);
            paras[8] = new SqlParameter("@QUANTITY_UM", locInvDO.QUANTITY_UM == String.Empty ? DBNull.Value : (object)locInvDO.QUANTITY_UM);
            paras[9] = new SqlParameter("@INVENTORY_STS", locInvDO.INVENTORY_STS == String.Empty ? DBNull.Value : (object)locInvDO.INVENTORY_STS);
            paras[10] = new SqlParameter("@AGING_DATE", locInvDO.AGING_DATE == DateTime.MinValue ? DBNull.Value : (object)locInvDO.AGING_DATE);
            paras[11] = new SqlParameter("@USER_STAMP", locInvDO.USER_STAMP == String.Empty ? DBNull.Value : (object)locInvDO.USER_STAMP);
            paras[12] = new SqlParameter("@DATE_TIME_STAMP", locInvDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)locInvDO.DATE_TIME_STAMP);
            paras[13] = new SqlParameter("@WAREHOUSE", locInvDO.WAREHOUSE == String.Empty ? DBNull.Value : (object)locInvDO.WAREHOUSE);
            paras[14] = new SqlParameter("@LOCATION", locInvDO.LOCATION == String.Empty ? DBNull.Value : (object)locInvDO.LOCATION);
            paras[15] = new SqlParameter("@ITEM_DESC", locInvDO.ITEM_DESC == String.Empty ? DBNull.Value : (object)locInvDO.ITEM_DESC);
            paras[16] = new SqlParameter("@LPN", locInvDO.LPN == String.Empty ? DBNull.Value : (object)locInvDO.LPN);
            paras[17] = new SqlParameter("@PARENT_LPN", locInvDO.PARENT_LPN == String.Empty ? DBNull.Value : (object)locInvDO.PARENT_LPN);
            paras[18] = new SqlParameter("@ATTRIBUTE1", locInvDO.ATTRIBUTE1 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE1);
            paras[19] = new SqlParameter("@ATTRIBUTE2", locInvDO.ATTRIBUTE2 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE2);
            paras[20] = new SqlParameter("@ATTRIBUTE3", locInvDO.ATTRIBUTE3 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE3);
            paras[21] = new SqlParameter("@ATTRIBUTE4", locInvDO.ATTRIBUTE4 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE4);
            paras[22] = new SqlParameter("@ATTRIBUTE5", locInvDO.ATTRIBUTE5 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE5);
            paras[23] = new SqlParameter("@ATTRIBUTE6", locInvDO.ATTRIBUTE6 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE6);
            paras[24] = new SqlParameter("@ATTRIBUTE7", locInvDO.ATTRIBUTE7 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE7);
            paras[25] = new SqlParameter("@ATTRIBUTE8", locInvDO.ATTRIBUTE8 == String.Empty ? DBNull.Value : (object)locInvDO.ATTRIBUTE8);

            paras[0] .DbType = DbType.Decimal;
            paras[1] .DbType = DbType.String;
            paras[2] .DbType = DbType.String;
            paras[3] .DbType = DbType.String;
            paras[4] .DbType = DbType.String;
            paras[5] .DbType = DbType.Decimal;
            paras[6] .DbType = DbType.Decimal;
            paras[7] .DbType = DbType.Decimal;
            paras[8] .DbType = DbType.String;
            paras[9] .DbType = DbType.String;
            paras[10].DbType = DbType.Date;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.Date;
            paras[13].DbType = DbType.String;
            paras[14].DbType = DbType.String;
            paras[15].DbType = DbType.String;
            paras[16].DbType = DbType.String;
            paras[17].DbType = DbType.String;
            paras[18].DbType = DbType.String;
            paras[19].DbType = DbType.String;
            paras[20].DbType = DbType.String;
            paras[21].DbType = DbType.String;
            paras[22].DbType = DbType.String;
            paras[23].DbType = DbType.String;
            paras[24].DbType = DbType.String;
            paras[25].DbType = DbType.String;

            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(LocationInventoryDO locInvDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_LOCATION_INV", locInvDO.INTERNAL_LOCATION_INV);
            return paras;
        }

        #endregion


        public static List<LocationInventoryDO> GetListByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam, stOrderBy);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumns(Session sess, List<KeyValuePair<string, KeyValuePair<string, object>>> dcParam, string stOrderBy)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, KeyValuePair<string, object>> de in dcParam)
                {
                    sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(de.Value.Key).Append("@Value").Append(i);
                    pars[i] = new SqlParameter("@Value" + i, de.Value.Value);
                    i++;
                }

                if (!StringManager.IsEmpty(stOrderBy))
                {
                    sbSQL.Append(stOrderBy);
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
